﻿using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using Indeco.G24.Entities;
using Indeco.G24.Services;

namespace Indeco.G24.Website.User
{
    public partial class AddProvider : System.Web.UI.UserControl
    {
        NhaCungCapService nhaCungCapService = new NhaCungCapService();
        NguoiDungService nguoiDungService = new NguoiDungService();

        /// <summary>
        /// 
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void Page_Load(object sender, EventArgs e)
        {
            //try
            //{
            //    lblError.Text = Membership.GetUser().UserName;
            //    if (Membership.GetUser().UserName == "")
            //        Response.Redirect("~/Login.aspx");
            //}
            //catch
            //{
            //    Response.Redirect("~/Login.aspx");
            //}

        }
        /// <summary>
        /// 
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void btnHuyBo_Click(object sender, EventArgs e)
        {
            Response.Redirect("~/User");
        }
        /// <summary>
        /// 
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void btnFinishRegisterProvider_Click(object sender, EventArgs e)
        {
            try
            {
                if (Membership.GetUser().UserName == "")
                    Response.Redirect("~/Login.aspx");
            }
            catch
            {
                Response.Redirect("~/Login.aspx");
            }
            lblErrorNhaCungCap.Text = "";
            lblErrorEmail.Text = "";
            lblError.Text = "";
            if (Page.IsValid)
            {
                NhaCungCap checkNhaCungCap = nhaCungCapService.GetByNhaCungCap(txtNhaCungCap.Text.Trim());
                TList<NhaCungCap> checkNhaCungCapByEmail = nhaCungCapService.Find("Email = '" + txtEmail.Text.Trim() + "'");
                if (checkNhaCungCap == null && checkNhaCungCapByEmail.Count == 0 && listSanPhamDanhMuc.SelectedItem != null)
                {
                    try
                    {
                        NhaCungCap nhaCungCap = new NhaCungCap();
                        nhaCungCap.IDNhaCungCap = Guid.NewGuid();
                        nhaCungCap.NhaCungCap = txtNhaCungCap.Text.Trim();
                        nhaCungCap.Ten = txtTen.Text.Trim();
                        nhaCungCap.DiaChi = txtDiaChi.Text.Trim();
                        nhaCungCap.DienThoai = txtDienThoai.Text.Trim();
                        nhaCungCap.Fax = txtFax.Text.Trim();
                        nhaCungCap.Email = txtEmail.Text.Trim();
                        //*********************
                        nhaCungCap.ChungThuc = false;
                        nhaCungCap.KichHoat = true;
                        nhaCungCap.CamHoatDong = false;
                        nhaCungCap.Xoa = false;
                        nhaCungCap.Vitri = 0;
                        nhaCungCap.ThoiDiemTruyCap = DateTime.Now;
                        nhaCungCap.ThoiDiemThoat = DateTime.Now;
                        nhaCungCap.ThoiDiemCapNhat = DateTime.Now;
                        nhaCungCap.ThoiDiemChinhSua = DateTime.Now;
                        //*********************
                        NguoiDungNhaCungCap nguoiDungNhaCungCap = new NguoiDungNhaCungCap();
                        TList<NguoiDung> nguoiDung = nguoiDungService.Find("TenSuDung = '" + Membership.GetUser().UserName + "'");
                        nguoiDungNhaCungCap.IDNguoiDung = nguoiDung[0].IDNguoiDung;
                        nguoiDungNhaCungCap.IDNhaCungCap = nhaCungCap.IDNhaCungCap;
                        NguoiDungNhaCungCapService nguoiDungNhaCungCapService = new NguoiDungNhaCungCapService();
                        if (nhaCungCapService.Insert(nhaCungCap) && nguoiDungNhaCungCapService.Insert(nguoiDungNhaCungCap))
                        {
                            NhaCungCapDiaBan nhaCungCapDiaBan = new NhaCungCapDiaBan();
                            nhaCungCapDiaBan.IDDiaBan = new Guid(listDiaBan.SelectedValue);
                            nhaCungCapDiaBan.IDNhaCungCap = nhaCungCap.IDNhaCungCap;
                            nhaCungCapDiaBan.ViTri = 1;
                            nhaCungCapDiaBan.TrangThai = true;
                            NhaCungCapDiaBanService nhaCungCapDiaBanService = new NhaCungCapDiaBanService();
                            if (!nhaCungCapDiaBanService.Insert(nhaCungCapDiaBan))
                                lblError.Text = "Địa bàn chưa cập nhật<br>";
                            NhaCungCapSanPhamDanhMuc nhaCungCapSanPhamDanhMuc = new NhaCungCapSanPhamDanhMuc();
                            int i = 1;
                            foreach (ListItem item in listSanPhamDanhMuc.Items)
                            {
                                if (item.Selected)
                                {
                                    nhaCungCapSanPhamDanhMuc.IDSanPhamDanhMuc = new Guid(item.Value);
                                    nhaCungCapSanPhamDanhMuc.IDDiaBan = new Guid(listDiaBan.SelectedValue);
                                    nhaCungCapSanPhamDanhMuc.IDNhaCungCap = nhaCungCap.IDNhaCungCap;
                                    nhaCungCapSanPhamDanhMuc.ViTri = i;
                                    nhaCungCapSanPhamDanhMuc.TrangThai = true;
                                    nhaCungCapSanPhamDanhMuc.HienThi = true;
                                    nhaCungCapSanPhamDanhMuc.ThoiDiemCapNhat = DateTime.Now;
                                    nhaCungCapSanPhamDanhMuc.ThoiDiemChinhSua = DateTime.Now;
                                    NhaCungCapSanPhamDanhMucService nhaCungCapSanPhamDanhMucService = new NhaCungCapSanPhamDanhMucService();
                                    if (!nhaCungCapSanPhamDanhMucService.Insert(nhaCungCapSanPhamDanhMuc))
                                        lblError.Text += "Danh mục sản phẩm <b>" + item.Text + "</b> chưa cập nhật<br>";
                                    else
                                        i = i + 1;
                                }
                            }
                            Session["IDNhaCungCap"] = nhaCungCap.IDNhaCungCap;
                            Response.Redirect("~/Price/Admin/PriceList.aspx");
                         
                        }
                    }
                    catch
                    { }
                }
                else
                {
                    if (checkNhaCungCap != null)
                        lblErrorNhaCungCap.Text = "Tên <b>" + checkNhaCungCap.NhaCungCap + "</b> đã có, bạn hãy chọn lại!";
                    if (checkNhaCungCapByEmail.Count > 0)
                        lblErrorEmail.Text = "Địa chỉ E-mail <b>" + txtEmail.Text + "</b> đã đăng ký.";
                    if (listSanPhamDanhMuc.SelectedItem == null)
                    {
                        lblError.Text = "Chọn danh mục sản phẩm";
                    }
                }
            }
        }


    }
}